# 综合练习 爬取豆瓣电影
import requests
import json

url = "https://movie.douban.com/j/search_subjects"

# 请求头
header = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36"
}

# 重新封装请求参数
param = {
    "type": "movie",
    "tag": "热门",
    "page_limit": "50",
    "page_start": "0",
}

res = requests.get(url, params=param, headers=header)

# 将请求到的数据存入json文件
with open("movie.json", 'w', encoding='utf-8') as f:
    f.write(json.dumps(res.json(), ensure_ascii=False))

print(res.json())
res.close()  # 关闭请求
